﻿using AutomaticUpdates;
using CustomControl;
using PublicTools;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace InstanceGadget
{
    public partial class InstanceGadgetFrm : Form
    {
        public InstanceGadgetFrm()
        {
            //为了自定义设置界面的标题，边框样式，我们通常会把winform窗体的边
            //框属性设置为None，但去掉边框的窗体在最大化时会挡住电脑桌面的任务栏，
            // 但任务栏的时间、输入法等我们仍然需要用到，因此任务栏必须显示出来。
            this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
            InitializeComponent();
        }

        private static Panel Var_Panel = new Panel();
        private static PictureBox Var_Pict = new PictureBox();
        private static int Var_i = 0;
        private Font Var_Font = new Font("宋体", 9);
        private NetStruct[] myNetStruct;
        private NetInfo myNetInfo;

        private void timer1_Tick(object sender, EventArgs e)
        {
            NetStruct NStruct = myNetStruct[0];
            toolStripStatusLabel5.Text = "网络流量[接收:" + NStruct.Receive + "B 发送:" + NStruct.Send + "B]";
            toolStripStatusLabel1.Text = string.Format("【系统当前时间为：") + DateTime.Now.ToLongTimeString() + "】";//动态显示系统时间
        }

        private void InstanceGadgetFrm_Load(object sender, EventArgs e)
        {
            InitializeLoadResources();
            myNetInfo = new NetInfo();
            myNetStruct = myNetInfo.myNetStructs;
            myNetInfo.GetInfo(myNetStruct[0]);
            DateTime appStartTime = Process.GetCurrentProcess().StartTime;
            string startTime = string.Format("【软件启动时间：{0}】", appStartTime.ToString("MM-dd HH:mm"));
            toolStripStatusLabel2.Text = startTime;
            toolStripStatusLabel3.Text = "【" + "电脑计算机名称为：" + CommTools.LocalHostName + "】";
            DateTime dt1 = DateTime.Now;
            DateTime dt2 = DateTime.Parse("2019-10-9 14:20:45");
            TimeSpan ts = dt1 - dt2;
            toolStripStatusLabel6.Text = "软件共存时间：" + ts.Days.ToString() + "天";
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            //if (MessageBox.Show(" 确 定 退 出 ？", "提 示", MessageBoxButtons.YesNo, MessageBoxIcon.None) == DialogResult.Yes)
            //{
            //    Environment.Exit(0);
            //}
            //else
            //{
            //    //e.Cancel = true;
            //}
            Form _exit = Application.OpenForms["Exit"]; //查找是否打开过Exit窗体
            if ((_exit == null) || (_exit.IsDisposed))//如果没有打开过
            {
                Exit _exits = new Exit();
                _exits.ShowDialog(); //打开子窗体出来
            }
            else
            {
                _exit.Activate(); //如果已经打开过就让其获得焦点
                                  // _exit.WindowState = FormWindowState.Normal;//使Form恢复正常窗体大小
            }
        }

        #region 判断计算机（软件）是否连接网络

        [DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
        public extern static bool InternetGetConnectedState(out int conState, int reder);

        //参数说明 constate 连接说明 ，reder保留值
        public bool IsConnectedToInternet()
        {
            int Desc = 0;
            return InternetGetConnectedState(out Desc, 0);
        }

        /// <summary>
        /// 状态栏显示进度条和刷新网络连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (IsConnectedToInternet())
            {
                toolStripStatusLabel4.Text = "网络已经连接";
            }
            else
            {
                toolStripStatusLabel4.Text = "网络已经断开";
            }
        }

        #endregion 判断计算机（软件）是否连接网络

        /// <summary>
        /// 初始化加载资源
        /// </summary>
        private void InitializeLoadResources()
        {
            pictureBox2.Image = null;
            pictureBox2.Image = Properties.Resources.Green_Up;
            pictureBox3.Image = null;
            pictureBox3.Image = Properties.Resources.Green_Up;
            pictureBox4.Image = null;
            pictureBox4.Image = Properties.Resources.Green_Up;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            Var_i = Convert.ToInt16(((PictureBox)sender).Tag.ToString());
            switch (Var_i)
            {
                case 1:
                    {
                        Var_Panel = panel_Gut_1;
                        Var_Pict = pictureBox2;
                        break;
                    }
                case 2:
                    {
                        Var_Panel = panel_Gut_2;
                        Var_Pict = pictureBox3;
                        break;
                    }
                case 3:
                    {
                        Var_Panel = panel_Gut_3;
                        Var_Pict = pictureBox4;
                        break;
                    }
            }
            if (Convert.ToInt16(Var_Panel.Tag.ToString()) == 0 || Convert.ToInt16(Var_Panel.Tag.ToString()) == 2)
            {
                Var_Panel.Tag = 1;//隐藏标识
                Var_Pict.Image = null;
                Var_Pict.Image = Properties.Resources.Green_Down;
                Var_Panel.Visible = false;
            }
            else
            {
                if (Convert.ToInt16(Var_Panel.Tag.ToString()) == 1)
                {
                    Var_Panel.Tag = 2;//显示标识
                    Var_Pict.Image = null;
                    Var_Pict.Image = Properties.Resources.Green_Up;
                    Var_Panel.Visible = true;
                }
            }
        }

        #region 主界面左侧栏目的导航栏panel_Master控件的刷新

        /// <summary>
        /// 主界面左侧栏目的导航栏panel_Master控件的刷新
        /// </summary>
        private void MasterControlsClear()
        {
            this.panel_Master.Controls.Clear();
        }

        #endregion 主界面左侧栏目的导航栏panel_Master控件的刷新

        #region 主界面左侧栏目的导航栏单击事件

        /// <summary>
        /// 自动更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label2_Click(object sender, EventArgs e)
        {
            AutomaticUpdatesFrm automaticUpdates = new AutomaticUpdatesFrm();
            automaticUpdates.Show();
        }

        /// <summary>
        /// 窗体技巧
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label1_Click(object sender, EventArgs e)
        {
            MasterControlsClear();
            FormControls formControl = new FormControls();
            panel_Master.Controls.Add(formControl);
            panel_Master.Size = formControl.Size;
        }

        /// <summary>
        /// 控件操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label3_Click(object sender, EventArgs e)
        {
            MasterControlsClear();
            ControlOperating controlOperating = new ControlOperating();
            panel_Master.Controls.Add(controlOperating);
            panel_Master.Size = controlOperating.Size;
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label4_Click(object sender, EventArgs e)
        {
            MasterControlsClear();
            QrCodeGenerator qrCodeGenerator = new QrCodeGenerator();
            panel_Master.Controls.Add(qrCodeGenerator);
            panel_Master.Size = qrCodeGenerator.Size;
        }

        /// <summary>
        /// 小工具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label5_Click(object sender, EventArgs e)
        {
            MasterControlsClear();
            SmallTools smallTools = new SmallTools();
            panel_Master.Controls.Add(smallTools);
            panel_Master.Size = smallTools.Size;
        }

        #endregion 主界面左侧栏目的导航栏单击事件

        #region 神奇的代码 解决winform闪烁问题
        //protected override CreateParams CreateParams => base.CreateParams;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        #endregion
    }
}